/*
 * Copyright © 2011 Breachwalls & Company Inc. and other authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.breachwalls.h2p.plugin.prescription.web.service.impl;

import javax.annotation.Resource;

import com.breachwalls.eap.core.exceptions.VerificationLogicException;
import com.breachwalls.h2p.platform.workflow.business.BusinessInstance;
import com.breachwalls.h2p.platform.workflow.process.ProcessService;
import com.breachwalls.h2p.plugin.prescription.domain.bl.PrescriptionBl;
import com.breachwalls.h2p.plugin.prescription.domain.bl.PrescriptionCommentBl;
import com.breachwalls.h2p.plugin.prescription.domain.bl.PrescriptionStatus;
import com.breachwalls.h2p.plugin.prescription.persistence.entity.Prescription;
import com.breachwalls.h2p.plugin.prescription.persistence.entity.PrescriptionComment;
import com.breachwalls.h2p.plugin.prescription.web.form.PrescriptionForm;
import com.breachwalls.h2p.plugin.prescription.web.service.PrescriptionFormService;
import com.breachwalls.h2p.plugin.prescription.workflow.variables.PrescriptionVariables;

/**
 * 処方箋登録フォームサービス.
 * @author Ken Todoroki
 * @version $Revision$
 */
public class PrescriptionFormServiceImpl implements PrescriptionFormService {

    @Resource
    private PrescriptionBl prescriptionBl;

    @Resource
    private PrescriptionCommentBl prescriptionCommentBl;

    @Resource
    private ProcessService processService;

    @Override
    public void execConfirmPrescriptionForm(PrescriptionForm form)
            throws VerificationLogicException {
    }

    @Override
    public void execCreatePrescriptionForm(PrescriptionForm form) throws VerificationLogicException {
        // 処方箋の登録
        Prescription prescription = form.getPrescription();
        prescription.setImageUrl("http://www.dummy.com/storage/images/hoge.img");
        prescription.setStatus(PrescriptionStatus.OPEN.name());
        prescriptionBl.createPrescription(prescription);

        // コメントの登録
        PrescriptionComment comment = form.getPrescriptionComment();
        prescriptionCommentBl.createPrescriptionComment(prescription, comment);

        // フローの開始
        BusinessInstance bi = new BusinessInstance();
        bi.setFlowName("PrescriptionProcess");
        bi.setId(prescription.getId());
        bi.put(PrescriptionVariables.PRESCRIPTION_ENTITY.name(), prescription);
        bi.put(PrescriptionVariables.PRESCRIPTION_COMMENT_ENTITY.name(), comment);
        processService.start(bi);
    }

}
